package com.wingsoft.work.controller;

import com.wingsoft.common.core.domain.R;
import com.wingsoft.work.dto.*;
import com.wingsoft.work.service.impl.ActivationServiceImpl;
import com.wingsoft.work.service.impl.UserTaskLogServiceImpl;
import com.wingsoft.work.service.impl.UserTaskServiceImpl;
import com.wingsoft.work.vo.ActivationVo;
import com.wingsoft.work.vo.UserTaskLogVo;
import com.wingsoft.work.vo.UserTaskVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import com.github.pagehelper.Page;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wingsoft.common.annotation.Log;
import com.wingsoft.common.core.controller.BaseController;
import com.wingsoft.common.core.domain.AjaxResult;
import com.wingsoft.common.enums.BusinessType;
import com.wingsoft.work.domain.UserVip;
import com.wingsoft.work.vo.UserVipVo;
import com.wingsoft.work.service.impl.UserVipServiceImpl;
import com.wingsoft.common.utils.poi.ExcelUtil;

/**
 * 用户套餐Controller
 *
 * @author hemj
 * @date 2025-10-20
 */
@Api(tags = "用户相关接口")
@RestController
@RequestMapping("/work/userVip")
public class UserVipController extends BaseController
{
    @Autowired
    private UserVipServiceImpl userVipService;
    @Autowired
    private ActivationServiceImpl activationService;
    @Autowired
    private UserTaskServiceImpl userTaskService;
    @Autowired
    private UserTaskLogServiceImpl userTaskLogService;


    /**
     * 分页查询用户套餐
     */
    //@PreAuthorize("@ss.hasPermi('work:userVip:list')")
    @PostMapping("/list")
    @ApiOperation("分页查询用户套餐")
    public R<List<UserVipVo>> list(@RequestBody UserVipDto userVipDto)
    {
        Page page = startPage();
        List<UserVipVo> list = userVipService.queryList(userVipDto);
        return getResDataTable(list,page);
    }

    /**
     * 删除用户套餐
     */
    @PreAuthorize("@ss.hasPermi('work:userVip:back')")
    @Log(title = "删除用户套餐", businessType = BusinessType.DELETE)
    @PostMapping("/back/{ids}")
    @ApiOperation("删除用户套餐")
    public R remove(@PathVariable Long[] ids)
    {
        userVipService.backByIdList(Arrays.asList(ids));
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('work:userVip:back')")
    @Log(title = "删除用户套餐", businessType = BusinessType.DELETE)
    @PostMapping("/backUid")
    @ApiOperation("删除用户套餐")
    public R backUid(@RequestBody UserVipDto userVipDto)
    {
        userVipService.backUid(userVipDto);
        return R.ok();
    }


    @PostMapping("/active")
    @ApiOperation("激活VIP")
    public R active(@RequestBody ActivationEntity activationEntity)
    {
        activationService.active(activationEntity);
        return R.ok();
    }

    @PostMapping("/isVip")
    @ApiOperation("获取用户的VIP信息")
    public R<UserVipVo> isVip()
    {
        UserVipVo userVipVo = userVipService.isVip();
        return R.ok(userVipVo);
    }

    @PostMapping(value = "/deleteTask")
    @ApiOperation("删除用户任务")
    public R deleteTask(@RequestBody UserTaskDto userTaskDto)
    {
        userTaskService.deleteTask(userTaskDto.getId());
        return R.ok();
    }

    @PostMapping("/query")
    @ApiOperation("获取激活历史")
    public R<List<ActivationVo>> queryActivation(@RequestBody ActivationDto activationDto)
    {
        activationDto.setUserId(getUserId());
        List<ActivationVo> list = activationService.queryList(activationDto);
        return R.ok(list);
    }

    @Log(title = "调用下载", businessType = BusinessType.INSERT)
    @PostMapping(value = "/downloadVideo")
    @ApiOperation("新增用户任务")
    public R downloadVideo(@RequestBody UserTaskDto userTaskDto)
    {
        return R.ok(userTaskService.downloadVideo(userTaskDto));
    }


    @Log(title = "新增用户任务", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addTask")
    @ApiOperation("新增用户任务")
    public R addTask(@RequestBody UserTaskDto userTaskDto)
    {
        return R.ok(userTaskService.saveCascade(userTaskDto));
    }


    @Log(title = "新增用户任务", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addTaskBatch")
    @ApiOperation("批量新增用户任务")
    public R addTaskBatch(@RequestBody UserTaskDto userTaskDto)
    {
        return R.ok(userTaskService.addTaskBatch(userTaskDto));
    }


    @PostMapping("/getTask")
    @ApiOperation("查询任务明细")
    public R<UserTaskVo> getTask(@RequestBody UserTaskDto userTaskDto)
    {
        UserTaskVo userTaskVo = userTaskService.getOneById(userTaskDto.getId());
        return R.ok(userTaskVo);
    }

    @PostMapping("/listTask")
    @ApiOperation("分页查询用户任务")
    public R<List<UserTaskVo>> list(@RequestBody UserTaskDto userTaskDto)
    {
        Page page = startPage();
        userTaskDto.setUserId(getUserId());
        userTaskDto.setDelFlag(0);
        List<UserTaskVo> list = userTaskService.queryList(userTaskDto);
        return getResDataTable(list,page);
    }


    @PostMapping("/listTaskLog")
    @ApiOperation("分页查询用户任务日志")
    public R<List<UserTaskLogVo>> listTaskLog(@RequestBody UserTaskLogDto userTaskLogDto)
    {
        Page page = startPage();
        userTaskLogDto.setUserId(getUserId());
        List<UserTaskLogVo> list = userTaskLogService.queryList(userTaskLogDto);
        return getResDataTable(list,page);
    }
}
